package com.hafu.swing;
import com.hafu.model.Student;
import com.hafu.service.StudentService;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class AddStudentDialog extends JDialog {
    private StudentService studentService;
    private StudentManagePanel parentPanel;

    public AddStudentDialog(StudentManagePanel parentPanel, StudentService studentService) {
        super((Frame) null, "添加学生", true);
        this.parentPanel = parentPanel;
        this.studentService = studentService;
        setSize(400, 300);
        setLocationRelativeTo(null);
        setLayout(new GridLayout(8, 2));

        JTextField idField = new JTextField();
        JTextField nameField = new JTextField();
        JComboBox<String> genderBox = new JComboBox<>(new String[]{"男", "女"});
        JTextField ageField = new JTextField();
        JTextField majorField = new JTextField();
        JTextField dateField = new JTextField();
        JTextField phoneField = new JTextField();

        add(new JLabel("学号:"));
        add(idField);
        add(new JLabel("姓名:"));
        add(nameField);
        add(new JLabel("性别:"));
        add(genderBox);
        add(new JLabel("年龄:"));
        add(ageField);
        add(new JLabel("专业:"));
        add(majorField);
        add(new JLabel("入学时间 (YYYY-MM-DD):"));
        add(dateField);
        add(new JLabel("联系电话:"));
        add(phoneField);

        JButton saveButton = new JButton("保存");
        JButton cancelButton = new JButton("取消");
        saveButton.addActionListener(this::saveStudent);
        cancelButton.addActionListener(e -> dispose());

        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);
        add(buttonPanel);
    }

    private void saveStudent(ActionEvent e) {
        try {
            String studentId = ((JTextField) getComponent(1)).getText().trim();
            String name = ((JTextField) getComponent(3)).getText().trim();
            String gender = (String) ((JComboBox<?>) getComponent(5)).getSelectedItem();
            int age = Integer.parseInt(((JTextField) getComponent(7)).getText().trim());
            String major = ((JTextField) getComponent(9)).getText().trim();
            String enrollmentDateStr = ((JTextField) getComponent(11)).getText().trim();
            String phone = ((JTextField) getComponent(13)).getText().trim();

            // 将字符串日期转换为 java.util.Date 类型
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            sdf.setLenient(false); // 严格匹配日期格式
            java.util.Date enrollmentDate = sdf.parse(enrollmentDateStr);

            Student student = new Student(studentId, name, gender, age, major, enrollmentDate, phone);
            if (studentService.addStudent(student)) {
                JOptionPane.showMessageDialog(this, "添加成功");
                parentPanel.refreshData();
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "添加失败", "错误", JOptionPane.ERROR_MESSAGE);
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "年龄必须为数字", "输入错误", JOptionPane.WARNING_MESSAGE);
        } catch (java.text.ParseException ex) {
            JOptionPane.showMessageDialog(this, "日期格式不正确，应为 YYYY-MM-DD", "输入错误", JOptionPane.WARNING_MESSAGE);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "发生未知错误：" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}